package com.edu.policy.controller;

import com.edu.policy.common.api.CommonResult;
import com.edu.policy.dao.UmsMenuNode;
import com.edu.policy.domin.PageDto;
import com.edu.policy.model.UmsMenu;
import com.edu.policy.service.AppUmsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台菜单管理Controller
 * Created by chz on 2020/2/4.
 */
@Controller
@Api(tags = "UmsMenuController", description = "后台菜单管理")
@RequestMapping("/menu")
@RequiredArgsConstructor
public class UmsMenuController {

    private final AppUmsMenuService menuService;

    @ApiOperation("添加后台菜单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> create(@RequestBody UmsMenu umsMenu) {
        boolean count = menuService.save(umsMenu);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> update(@PathVariable Long id,
                                                 @RequestBody UmsMenu umsMenu) {
        umsMenu.setId(id);
        boolean count = menuService.updateById(umsMenu);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取菜单详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMenu> getItem(@PathVariable Long id) {
        UmsMenu umsMenu = menuService.getById(id);
        return CommonResult.success(umsMenu);
    }

    @ApiOperation("根据ID删除后台菜单")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> delete(@PathVariable Long id) {
        boolean count = menuService.removeById(id);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询后台菜单")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageDto<UmsMenu>> list(@PathVariable Long parentId,
                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                               @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage) {
        PageDto<UmsMenu> menuList = menuService.list(parentId, pageSize, currentPage);
        return CommonResult.success(menuList);
    }

    @ApiOperation("树形结构返回所有菜单列表")
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsMenuNode>> treeList() {
        List<UmsMenuNode> list = menuService.treeList();
        return CommonResult.success(list);
    }

    @ApiOperation("修改菜单显示状态")
    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) {
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setId(id);
        umsMenu.setHidden(hidden);
        boolean count = menuService.updateById(umsMenu);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }
}
